package com.atguigu.chapter05.transform;

import com.atguigu.bean.WaterSensor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.util.ArrayList;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/6/9 13:47
 */
public class Flink08_RollingAgg {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        
        ArrayList<WaterSensor> waterSensors = new ArrayList<>();
        waterSensors.add(new WaterSensor("sensor_1", 1607527992000L, 20));
        waterSensors.add(new WaterSensor("sensor_1", 1607527994000L, 60));
        waterSensors.add(new WaterSensor("sensor_1", 1607527996000L, 60));
        waterSensors.add(new WaterSensor("sensor_2", 1607527993000L, 10));
        waterSensors.add(new WaterSensor("sensor_2", 1607527995000L, 30));
        // select id,  sum(vc) from t group by id
        env
            .fromCollection(waterSensors)
            //            .keyBy( value -> value.getId())
            .keyBy(WaterSensor::getId)   // 方法引用
            //            .sum("vc")
            //            .max("vc")
            //.min("vc")
            .maxBy("vc", true)
            
            .print();
        
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
/*
1. 在flink中, 聚合必须是在keyBy之后
2. 聚合之后, 非keyBy和聚合字段,取的是碰到的第一个

3. maxBy, minBy这个算子, 其他的字段取值是随着最大和最小

4. first这个布尔值, 用来控制当vc相等的时候, 是取第一个还是最新的

总结: 简单滚动聚合
    sum max min
    maxBy minBy
 */